//
//  WYMVisitorView.swift
//  iOS11weibo
//
//  Created by 王应明 on 16/7/19.
//  Copyright © 2016年 DescendantsOftheSun. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol WYMVisitorViewDelegate: NSObjectProtocol {
   //2.定义代理方法
    //注册点击
    func visitorViewDidRegiserClick()
    
    //登录点击
    func visitorViewDidLoginClick()
}

class WYMVisitorView: UIView {
    
    //3.定义代理属性 oc id<WYMVisitorViewDelegate>
    weak var delegate: WYMVisitorViewDelegate?
    
    //从一个view从storyboar或xib，系统认为既能用代码实现也要能从storyboat或xib实现
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //实现init（frame）
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 237/256.0, alpha: 1)
        self.setupUI()
    }
    
    //公开的方法
    func setupVisitorInfo(imageName: String, message: String) {
        houseView.hidden = true
        self.sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置参数
        //旋转多少度
        anim.toValue = 2 * M_PI
        //旋转一圈需要多少时间
        anim.duration = 20
        //旋转时长
        anim.repeatCount = MAXFLOAT
        //切换控制器时使核心动画可以继续运行
        anim.removedOnCompletion = false
        
        //将核心动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    private func setupUI() {
        //子控件添加到父控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        //设置约束
        //使用纯代码约束是需要关闭Autoresizing的影响
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //设置iconview的x约束添加到view上 237
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置iconView的y
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //设置coverView的x约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置coverView的Y约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置小房子的x值
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置小房子的Y值
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置lable的Y约束
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        //设置lable的x约束
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置lable的top到iconview的底部距离
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //设置注册按钮X的约束
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //设置注册按钮Y的约束（Top到message底部的间隔）
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //设置注册按钮的宽度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        //设置注册按钮的高度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        //设置登录按钮X的约束
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //设置登录按钮Top到message底部的间隔
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //设置登录按钮的宽度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //设置登录按钮的高度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    //MARK:--点击事件
    @objc private func registerButtonClick() {
        
//        print("点击注册点击")只有控制器才能moda
        delegate?.visitorViewDidRegiserClick()
    }
    @objc private func loginButtonClick() {
//        print("点击登录登录")只有控制器才能moda
        delegate?.visitorViewDidLoginClick()
    }
    
    
    //MARK:--懒加载
    ///转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///遮罩
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息lebel
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel(frame: CGRectZero)
        
        //设置
        label.font = UIFont.systemFontOfSize(15)
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "关注一些人,看看有什么惊喜!关注一些人,看看有什么惊喜!关注一些人,看看有什么惊喜!关注一些人,看看有什么惊喜!关注一些人,看看有什么惊喜!"
        //让控件size根据内容自己调整
        label.sizeToFit()
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置点击事件
        button.addTarget(self, action: #selector(WYMVisitorView.registerButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置登录按钮
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: #selector(WYMVisitorView.loginButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    
    
}
